package nl.nickthissen.remindme.activities;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;

import com.activeandroid.query.Select;

import java.util.ArrayList;

import nl.nickthissen.remindme.R;
import nl.nickthissen.remindme.common.CategoryColors;
import nl.nickthissen.remindme.common.SaveResult;
import nl.nickthissen.remindme.data.Category;
import nl.nickthissen.remindme.data.ReminderItem;
import nl.nickthissen.remindme.views.ColorButton;

/**
 * Created by nthissen on 11/06/13.
 */
public class EditCategoryActivity extends EditActivity<Category>
{
    private EditText txtName, txtDescr;
    private LinearLayout colorLayout;
    private AlertDialog dialog;

    private int color;

    private Category category;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.edit_category);

        txtName = (EditText) this.findViewById(R.id.txtName);
        txtDescr = (EditText) this.findViewById(R.id.txtDescr);
        colorLayout = (LinearLayout) this.findViewById(R.id.colorLayout);

        colorLayout.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                chooseColor();
            }
        });

        long id = this.getItemId();

        category = new Select().from(Category.class).where("Id=?", id).executeSingle();
        if (category == null) category = new Category();

        txtName.setText(category.name);
        txtDescr.setText(category.description);
        setColor(category.color);
    }

    public static Intent getIntent(Context context, long id)
    {
        Intent i = new Intent(context, EditCategoryActivity.class);
        i.putExtra(ITEM_ID, id);
        return i;
    }

    private void chooseColor()
    {
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.color_picker, null);

        ArrayList<Integer> colors = CategoryColors.colors;
        TableLayout table = (TableLayout) layout.findViewById(R.id.table);
        table.setStretchAllColumns(true);
        int columns = 4;
        int rows = (int)Math.ceil((double)colors.size() / columns);

        for (int i = 0; i < rows; i++)
        {
            TableRow row = new TableRow(this);
            row.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.MATCH_PARENT));

            for (int n = 0; n < columns; n++)
            {
                int index = i*columns + n;
                if (index >= colors.size()) break;

                ColorButton btn = new ColorButton(this);
                final int c = colors.get(index);
                btn.setColor(c);

                btn.setLayoutParams(new TableRow.LayoutParams(40, 40));

                btn.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        setColor(c);
                    }
                });

                row.addView(btn);
            }

            table.addView(row);
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(layout);
        builder.setTitle("Pick a color");
        dialog = builder.show();
    }

    private void setColor(int c)
    {
        color = c;
        colorLayout.setBackgroundColor(c);

        if (dialog != null) dialog.dismiss();
    }

    @Override
    public SaveResult onSave()
    {
        Editable name = txtName.getText();
        Editable descr = txtDescr.getText();

        if (name == null || TextUtils.isEmpty(name))
        {
            return SaveResult.error("Name cannot be empty!");
        }

        category.name = name.toString();
        category.color = color;
        if (descr == null)
            category.description = "";
        else
            category.description = descr.toString();

        category.save();
        return SaveResult.ok();
    }

    @Override
    public void onFinished()
    {
        this.finish();
    }

}
